<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>08.Materials(MeshLambertMaterial)</title>
    <style>
        body {
            margin: 0;
        }
    </style>
</head>

<body>
    <script src="build/three.js"></script>
    <script>
        //生成场景
        const scene = new THREE.Scene();
        //设置相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 2;
        //设置渲染器
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        //renderer.setClearColor(0xFFFFFF);
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        //生成模型
        const geometry = new THREE.BoxGeometry();
        const material = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
        const cube = new THREE.Mesh(geometry, material);

        const wireframeMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000, wireframe: true });
        const cubeWireframe = new THREE.Mesh(geometry, wireframeMaterial);
        //设置场景
        const group = new THREE.Group()
        group.add(cube)
        group.add(cubeWireframe)
        scene.add(group);
        //设置光照
        const light = new THREE.AmbientLight(0x111111);
        scene.add(light);
        const pointLight = new THREE.PointLight(0xFFFFFF, 1, 100);
        pointLight.position.set(5, 5, 10);
        scene.add(pointLight);

        // 生成TextureLoader
        const loader = new THREE.TextureLoader();
        // 加载纹理图
        loader.load(
            'texture/wood.jpg',
            // onLoad callback
            function (texture) {
                console.log(texture)
                material.map=texture
                material.needsUpdate=true
            },
            // onProgress callback currently not supported
            undefined,
            // 出错处理
            function (err) {
                console.error('An error happened.');
            }
        );

        function animate() {
            group.rotation.x += 0.005;
            group.rotation.y += 0.005;
            group.rotation.z += 0.005;
            renderer.render(scene, camera);
            requestAnimationFrame(animate);
        }
        //开始动画
        animate()
    </script>
</body>

</html>